package com.coda.controller.user;

import com.coda.common.Result;
import com.coda.constant.MessageConstant;
import com.coda.context.BaseContext;
import com.coda.pojo.CommentReply;
import com.coda.service.CommentReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 评论回复
 */
@RestController
@RequestMapping("/user/commentReply")
public class CommentReplyController {

    @Autowired
    private CommentReplyService commentReplyService;

    /**
     * 回复评论
     */
    @PutMapping
    public Result commentReply(@RequestBody CommentReply reply){
        reply.setUserReplyId(BaseContext.getCurrentId());
        reply.setCreateTime(LocalDateTime.now());
        int result = commentReplyService.commentReply(reply);
        return result > 0 ? Result.success() : Result.error(MessageConstant.UPVOTE_ERR);
    }

    /**
     * 点赞
     */
    @PostMapping("/{id}")
    public Result upvote(@PathVariable Integer id) {
        int result = commentReplyService.upvote(id);
        return result > 0 ? Result.success() : Result.error(MessageConstant.UPVOTE_ERR);
    }
}
